const axios = require('axios')
var APP_ID = '25669578'
var API_KEY = 'ffx6UtT85XpB6UnNcHbSbCfS'
var SECRET_KEY = 'UPdZXPlEwDzqag81F43wISA6P2tIFdPb'
class BaiduFaceApi {
  // 获取token
  async getToken(ctx, next) {
    try {
      const url = `https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=${API_KEY}&client_secret=${SECRET_KEY}`
      const result = await axios.get(url)
      if (ctx.req.token) {
        await next()
        return
      }
      ctx.req.token = result.data.access_token
      await next()
    } catch (error) {
      console.log('err', error)
    }
  }
  // 获取用户列表
  async faceUserList(access_token, groupId) {
    try {
      const url = `https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getusers?access_token=${access_token}`
      const result = await axios.post(url, {
        group_id: groupId,
      })
      return result.data.result.user_id_list
    } catch (error) {
      console.log('getUserList', error)
    }
  }
  // 组列表查询
  async faceSceneGroupList(access_token, body) {
    const url = `https://aip.baidubce.com/rest/2.0/face/v3/faceset/group/getlist?access_token=${access_token}`
    const result = await axios.post(url, {
      ...body,
    })
    return result.data
  }
  // 人脸注册
  async increment(access_token, image, imageType, groupId, userId) {
    try {
      const url = `https://aip.baidubce.com/rest/2.0/face/v3/faceset/user/add?access_token=${access_token}`
      const result = await axios.post(url, {
        image,
        image_type: imageType,
        group_id: groupId,
        user_id: userId,
        quality_control: 'HIGH',
      })
      return result.data
    } catch (err) {
      // 如果发生网络错误
      console.log('increment', err)
    }
  }

  // 人脸搜索1:N
  async faceSearch(access_token, image, imageType, groupId) {
    try {
      const url = `https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=${access_token}`
      console.time('faceSearch')
      const result = await axios.post(url, {
        image,
        image_type: imageType,
        group_id_list: groupId,
      })
      console.timeEnd('faceSearch')
      return result.data
    } catch (error) {
      console.log('faceSearch', error)
    }
  }
  // 人脸搜索 M:N
  async faceMultiSearch(access_token, image, imageType, groupId, body) {
    try {
      const url = `https://aip.baidubce.com/rest/2.0/face/v3/multi-search?access_token=${access_token}`
      console.time('faceMultiSearch')
      const result = await axios.post(url, {
        image,
        image_type: imageType,
        group_id_list: groupId,
        ...body,
      })
      console.timeEnd('faceMultiSearch')
      return result.data
    } catch (error) {
      console.log('faceMultiSearch', error)
    }
  }
  // 场景化-人脸注册
  async faceSceneAdd(access_token, image, imageType, groupId, userId, body) {
    const url = `https://aip.baidubce.com/rest/2.0/face/scene/faceset/user/add?access_token=${access_token}`
    const result = await axios.post(url, {
      image,
      image_type: imageType,
      group_id: groupId,
      user_id: userId,
      scene_type: 'sec',
      ...body,
    })
    return result.data
  }
  // 场景化-人脸搜索
  async faceSceneSearch(access_token, image, imageType, groupId) {
    const url = `https://aip.baidubce.com/rest/2.0/face/capture/search?access_token=${access_token}`
    console.time('faceSceneSearch')
    const result = await axios.post(url, {
      image,
      image_type: imageType,
      group_id_list: groupId,
      ...body,
    })
    console.timeEnd('faceSceneSearch')
    return result.data
  }
  // 在线图片活体
  async faceFaceverify(access_token, image, imageType, body) {
    const url = `https://aip.baidubce.com/rest/2.0/face/v3/faceverify?access_token=${access_token}`
    console.time('faceSceneSearch')
    const result = await axios.post(url, {
      image,
      image_type: imageType,
      ...body,
    })
    console.timeEnd('faceSceneSearch')
    return result.data
  }
}

module.exports = new BaiduFaceApi()
